#include "net.h"
#include "ui_net.h"
#include "keyboard.h"
#include  <sys/stat.h>
#include <QMessageBox>
#include <stdio.h>

#define COLUMN_SPACING 20
Net::Net(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Net)
{
    bool isDhcp;
    char  tmpIp[16]={0};
    char  tmpSubNet[16]={0};
    char  tmpGateWay[16]={0};
    char  tmpDNS[16]={0};
    char  tmpMac[20]={0};

    SetMainBacklight();

    ui->setupUi(this);
    ycapi = new Ycapi();
    ycapi = new Ycapi();
    ui->lineEditIpAdd->installEventFilter(this);
    ui->lineEditSubnetMask->installEventFilter(this);
    ui->lineEditDefaultGate->installEventFilter(this);
    ui->lineEditDNS->installEventFilter(this);

    ui->lineEditWifiSSID->installEventFilter(this);
    ui->lineEditWifiPwd->installEventFilter(this);

      ycapi->GetNetWorkCfg(0,&isDhcp,tmpIp,tmpSubNet, tmpGateWay,tmpDNS,tmpMac );

      ui->lineEditIpAdd->setText(QString(QLatin1String(tmpIp)));
      ui->lineEditSubnetMask->setText(QString(QLatin1String(tmpSubNet)));
      ui->lineEditDefaultGate->setText (QString(QLatin1String(tmpGateWay)));
      ui->lineEditDNS->setText(QString(QLatin1String(tmpDNS)));

      if(isDhcp)
      {
          ui->radioButtonDHCPSet->setChecked(true);
          ui->lineEditIpAdd->setDisabled(true);
          ui->lineEditSubnetMask->setDisabled(true);
          ui->lineEditDefaultGate->setDisabled(true);
          ui->lineEditDNS->setDisabled(true);
          ui->pushButtonApplyNet->setDisabled(true);
      }
      else
      {
          ui->radioButtonStaticIpSet->setChecked(true);
          ui->lineEditIpAdd->setDisabled(false);
          ui->lineEditSubnetMask->setDisabled(false);
          ui->lineEditDefaultGate->setDisabled(false);
          ui->lineEditDNS->setDisabled(false);
          ui->pushButtonApplyNet->setDisabled(false);
      }

      ui->lineEditWifiSSID->setAttribute(Qt::WA_InputMethodEnabled,false);
      ui->lineEditWifiPwd->setAttribute(Qt::WA_InputMethodEnabled,false);

      ui->lineEditIpAdd->setAttribute(Qt::WA_InputMethodEnabled,false);
      ui->lineEditSubnetMask->setAttribute(Qt::WA_InputMethodEnabled,false);
      ui->lineEditDefaultGate->setAttribute(Qt::WA_InputMethodEnabled,false);
      ui->lineEditDNS->setAttribute(Qt::WA_InputMethodEnabled,false);
}
void Net::SetMainBacklight()
{
     QPalette bgpal = palette();
     bgpal.setColor (QPalette::Background, QColor (230, 230 , 230, 255));
     bgpal.setColor (QPalette::Foreground, QColor (255,0,0,255));
     setPalette (bgpal);
}

Net::~Net()
{
    delete ui;
}

void Net::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
bool  Net::eventFilter(QObject *obj, QEvent *event)
{
    if((obj==ui->lineEditIpAdd)||(obj==ui->lineEditSubnetMask)
        ||(obj==ui->lineEditDefaultGate)||(obj==ui->lineEditDNS)
        ||(obj==ui->lineEditWifiSSID)||(obj==ui->lineEditWifiPwd)
        )
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            if(obj==ui->lineEditIpAdd)
            {
                 KeyBoard mKeyBoard(InputNet,ui->lineEditIpAdd->text());
                 connect(&mKeyBoard,SIGNAL(setvalue(QString)),obj,SLOT(setText(QString)));
                 mKeyBoard.exec();
             }
            else if (obj==ui->lineEditSubnetMask)
            {
                  KeyBoard mKeyBoard(InputNet,ui->lineEditSubnetMask->text());
                  connect(&mKeyBoard,SIGNAL(setvalue(QString)),obj,SLOT(setText(QString)));
                  mKeyBoard.exec();
              }
            else if(obj==ui->lineEditDefaultGate)
            {
                  KeyBoard mKeyBoard(InputNet,ui->lineEditDefaultGate->text());
                  connect(&mKeyBoard,SIGNAL(setvalue(QString)),obj,SLOT(setText(QString)));
                  mKeyBoard.exec();
            }
            else if (obj==ui->lineEditDNS)
            {
                 KeyBoard mKeyBoard(InputNet,ui->lineEditDNS->text());
                connect(&mKeyBoard,SIGNAL(setvalue(QString)),obj,SLOT(setText(QString)));
                mKeyBoard.exec();
            }
            else if(obj==ui->lineEditWifiSSID)
            {
                KeyBoard mKeyBoard(InputPassword,ui->lineEditWifiSSID->text());
                connect(&mKeyBoard,SIGNAL(setvalue(QString)),obj,SLOT(setText(QString)));
                mKeyBoard.exec();
            }
            else if(obj==ui->lineEditWifiPwd)
            {
                KeyBoard mKeyBoard(InputPassword,ui->lineEditWifiPwd->text());
                connect(&mKeyBoard,SIGNAL(setvalue(QString)),obj,SLOT(setText(QString)));
                mKeyBoard.exec();
            }
        }
    }

    return QDialog::eventFilter(obj,event);
}


void Net::on_radioButtonStaticIpSet_clicked()
{
    bool  isDhcp;
    char  tmpIp[16]={0};
    char  tmpSubNet[16]={0};
    char  tmpGateWay[16]={0};
    char  tmpDNS[16]={0};
    char  tmpMac[20]={0};

    ui->lineEditIpAdd->setDisabled(false);
    ui->lineEditSubnetMask->setDisabled(false);
    ui->lineEditDefaultGate->setDisabled(false);
    ui->lineEditDNS->setDisabled(false);
    ui->pushButtonApplyNet->setDisabled(false);
    ycapi->GetNetWorkCfg(0,&isDhcp,tmpIp,tmpSubNet, tmpGateWay,tmpDNS,tmpMac );

    ui->lineEditIpAdd->setText(QString(QLatin1String(tmpIp)));
    ui->lineEditSubnetMask->setText(QString(QLatin1String(tmpSubNet)));
    ui->lineEditDefaultGate->setText (QString(QLatin1String(tmpGateWay)));
    ui->lineEditDNS->setText(QString(QLatin1String(tmpDNS)));
}

void Net::on_radioButtonDHCPSet_clicked()
{
   ui->lineEditIpAdd->setDisabled(true);
   ui->lineEditSubnetMask->setDisabled(true);
   ui->lineEditDefaultGate->setDisabled(true);
   ui->lineEditDNS->setDisabled(true);
   ui->pushButtonApplyNet->setDisabled(true);

   ycapi->SetNetWork(0,true,NULL,NULL,NULL,NULL);

}

void Net::on_pushButtonApplyNet_clicked()
{
    ycapi->SetNetWork(0,false,ui->lineEditIpAdd->text().toLatin1().data(),
                                          ui->lineEditSubnetMask->text().toLatin1().data(),
                                          ui->lineEditDefaultGate->text().toLatin1().data(),
                                          ui->lineEditDNS->text().toLatin1().data());
}

void Net::on_pushButtonCloseWifi_clicked()
{
    system("killall -15 wpa_supplicant");
    system("killall -15 udhcpc");
    QMessageBox::information(this,"Note","wifi disconnect  ok!");
}

void Net::on_pushButtonConnWifi_clicked()
{
    char cmd[50]={0};
    QString str;
    QByteArray ba;
     QByteArray ba2;

    system("insmod /yctek/modules/wlan.ko");

     ba  =  ui->lineEditWifiSSID->text().toLatin1();
     ba2=  ui->lineEditWifiPwd->text().toLatin1();

    sprintf(cmd,"/usr/sbin/wlan.sh %s %s",  ba.data(),ba2.data());

    system(cmd);

    system("/sbin/udhcpc -i wlan0 -n");

     QMessageBox::information(this,"Note","wifi connect  ok!");
}

void Net::on_pushButtonYctekWifiTest_clicked()
{
    system("/usr/sbin/wifi.sh");
    QMessageBox::information(this,"Note","yctek test wifi connect  ok!");
}

void Net::on_pushButton3Gopen_clicked()
{
    system("cd /etc/ppp/peers/ ");
    system("pppd call  3gnet & ");
    system("sleep 10 ");

    QMessageBox::information(this,"Note","wcdma 3G connect  ok!");
}

void Net::on_pushButton3GClose_clicked()
{
    system("killall -15 pppd");
    QMessageBox::information(this,"Note","wcdma 3G  disconnect  ok!");
}

void Net::on_close_clicked()
{
    this->close();
}
